package com.css.zfzx.sjcj.modules.hddcwyGeomorStation.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.sjcj.modules.hddcwyGeomorStation.repository.HddcWyGeomorstationNativeRepository;
import com.css.zfzx.sjcj.modules.hddcwyGeomorStation.repository.HddcWyGeomorstationRepository;
import com.css.zfzx.sjcj.modules.hddcwyGeomorStation.repository.entity.HddcWyGeomorstationEntity;
import com.css.zfzx.sjcj.modules.hddcwyGeomorStation.service.HddcWyGeomorstationService;
import com.css.zfzx.sjcj.modules.hddcwyGeomorStation.viewobjects.HddcWyGeomorstationQueryParams;
import com.css.zfzx.sjcj.modules.hddcwyGeomorStation.viewobjects.HddcWyGeomorstationVO;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhangcong
 * @date 2020-12-01
 */
@Service
public class HddcWyGeomorstationServiceImpl implements HddcWyGeomorstationService {

	@Autowired
    private HddcWyGeomorstationRepository hddcWyGeomorstationRepository;
    @Autowired
    private HddcWyGeomorstationNativeRepository hddcWyGeomorstationNativeRepository;

    @Override
    public JSONObject queryHddcWyGeomorstations(HddcWyGeomorstationQueryParams queryParams , int curPage,
                                                int pageSize) {
        Page<HddcWyGeomorstationEntity> hddcWyGeomorstationPage =
                this.hddcWyGeomorstationNativeRepository.queryHddcWyGeomorstations(queryParams , curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(hddcWyGeomorstationPage);
        return jsonObject;
    }


    @Override
    public HddcWyGeomorstationEntity getHddcWyGeomorstation(String id) {
        HddcWyGeomorstationEntity hddcWyGeomorstation = this.hddcWyGeomorstationRepository.findById(id).orElse(null);
         return hddcWyGeomorstation;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyGeomorstationEntity saveHddcWyGeomorstation(HddcWyGeomorstationEntity hddcWyGeomorstation) {
        String uuid = UUIDGenerator.getUUID();
        hddcWyGeomorstation.setId(uuid);
        hddcWyGeomorstation.setCreateUser(PlatformSessionUtils.getUserId());
        hddcWyGeomorstation.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyGeomorstationRepository.save(hddcWyGeomorstation);
        return hddcWyGeomorstation;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyGeomorstationEntity updateHddcWyGeomorstation(HddcWyGeomorstationEntity hddcWyGeomorstation) {
        hddcWyGeomorstation.setUpdateUser(PlatformSessionUtils.getUserId());
        hddcWyGeomorstation.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyGeomorstationRepository.save(hddcWyGeomorstation);
        return hddcWyGeomorstation;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHddcWyGeomorstations(List<String> ids) {
        List<HddcWyGeomorstationEntity> hddcWyGeomorstationList = this.hddcWyGeomorstationRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(hddcWyGeomorstationList) && hddcWyGeomorstationList.size() > 0) {
            for(HddcWyGeomorstationEntity hddcWyGeomorstation : hddcWyGeomorstationList) {
                this.hddcWyGeomorstationRepository.delete(hddcWyGeomorstation);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

    @Override
    public BigInteger queryHddcWyGeomorstation(HddcWyGeomorstationQueryParams queryParams) {
        BigInteger bigInteger = hddcWyGeomorstationNativeRepository.queryHddcWyGeomorstation(queryParams);
        return bigInteger;
    }

}
